package edu.gyc.appshiro.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.gyc.appshiro.model.Student;
import edu.gyc.appshiro.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
@Slf4j
@RequestMapping("/stu")
public class StudentController {
    @Autowired
    private StudentService studentService;


    @RequestMapping("/add")
    public String addStu(Student student,Model model) {
        String msg = "";
       if(studentService.save(student)){
           msg="添加学生成功";
       }else {
           msg="添加学生失败";
       }
        model.addAttribute("msg", msg);
        return "/stu/query";
    }

    @RequestMapping("/del")
    public String delStu(Integer id,Model model) {
        String msg = "";
        if(studentService.removeById(id)){
            msg="删除学生成功";
        }else {
            msg="删除学生失败";
        }
        model.addAttribute("msg", msg);
        return "/stu/query";
    }

    @RequestMapping("/update")
    public String updateStu(Student student,Model model) {
        String msg = "";
        if(studentService.saveOrUpdate(student)){
            msg="更新学生成功";
        }else {
            msg="更新学生失败";
        }
        model.addAttribute("msg", msg);
        return "/stu/query";
    }

    @RequestMapping("/query")
    public String queryStu(@RequestParam(defaultValue = "1",value = "pageNo") Integer pageNo,
                           @RequestParam(defaultValue = "2" ,value = "pageSize") Integer pageSize,
                           HttpSession session) {
       log.info("called query student.");
        PageHelper.startPage(pageNo,pageSize);
        List<Student> students=studentService.lambdaQuery().orderByDesc(Student::getBirthday).list();
        PageInfo<Student> pageData = new PageInfo<>(students);
        session.setAttribute("stus", pageData.getList());
        session.setAttribute("currentPage", pageData.getPageNum());
        session.setAttribute("totalPage", pageData.getPages());

        return "admin/student";
    }

    @RequestMapping("/search")
    public String search(String key,
                         @RequestParam(defaultValue = "1",value = "pageNo") Integer pageNo,
                         @RequestParam(defaultValue = "2" ,value = "pageSize") Integer pageSize,
                         HttpSession session) {
        if (StringUtils.isNotBlank(key)) {
            session.setAttribute("searchKey",key);
        }else {
            key = (String) session.getAttribute("searchKey");
        }

        PageHelper.startPage(pageNo,pageSize);
      List<Student> students=  this.studentService.lambdaQuery()
                .like(Student::getName,key)
                .or()
                .like(Student::getGender,key)
                .or()
                .ge(Student::getBirthday,key).or()
                .like(Student::getMajor,key)
                .orderByDesc(Student::getId)
                .list();
        PageInfo<Student> pageData = new PageInfo<>(students);
        session.setAttribute("stus", pageData.getList());
        session.setAttribute("currentPage", pageData.getPageNum());
        session.setAttribute("totalPage", pageData.getPages());
        return "admin/stuSearch";
    }
}

